package com.ztk.threadbasic.excel.listener;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.ztk.threadbasic.excel.CompanyVO;
import com.ztk.threadbasic.excel.exception.CustomReaderExcelException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Data
@Service
@Scope("prototype")
public class CustomReadListener implements ReadListener<CompanyVO> {

    // 预算机构
    private String budgetCom;

    // 预算时间
    private String budgetDate;

    // 存放实体的List
    private List<CompanyVO> companyVOList = Lists.newArrayList();

    private static String STRING_TYPE = "STRING";

    private static String NUMBER_TYPE = "NUMBER";

    private int rowIndex = 1; // 当前行号

    private int startRow = 4; // 从第n行开始读取

    private static final int BATCH_SIZE = 500;

    /**
     * 读取excel中第一行数据
     *      获取第一行中第二列和第四列的数据值
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHead(Map headMap, AnalysisContext context) {
        log.info("开始解析excel第"+ rowIndex+"行！！！");
        // 虽然前四行是表头，但是由于只需要第一行的数据，所以，此处只处理解析第一行获取的数据
        if (rowIndex == 1) {
            Collection values = headMap.values();
            Iterator iterator = values.iterator();
            int count = 0;
            while (iterator.hasNext()) {
                ReadCellData readCellData = (ReadCellData) iterator.next();
                Integer columnIndex = readCellData.getColumnIndex();
                CellDataTypeEnum typeEnum = readCellData.getType();
                String typeEnumName = typeEnum.name();
                String stringValue;
                if (count == 1) {
                    if (STRING_TYPE.equalsIgnoreCase(typeEnumName)) {
                        stringValue = readCellData.getStringValue();
                        this.budgetCom = stringValue;
                        log.info("解析当前excel，获取预算机构为："+this.budgetCom);
                    }
                }
                if(count == 3){
                    if (STRING_TYPE.equalsIgnoreCase(typeEnumName)) {

                        stringValue = readCellData.getStringValue();

                        this.budgetDate = stringValue;

                        log.info("解析当前excel，获取预算时间为："+ this.budgetDate);

                    }

                    if (NUMBER_TYPE.equalsIgnoreCase(typeEnumName)) {

                        BigDecimal numberValue = readCellData.getNumberValue();

                        Date javaDate = DateUtil.getJavaDate(numberValue.doubleValue());

                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");

                        stringValue = simpleDateFormat.format(javaDate);

                        this.budgetDate = stringValue;

                        log.info("解析当前excel，获取预算时间为："+this.budgetDate);

                    }
                }
                count++;
            }
        }
        log.info("第"+rowIndex+"行解析结束");
        rowIndex++;
    }


    @Override
    public void invoke(CompanyVO companyVO, AnalysisContext analysisContext) {
        Integer rowIndex = analysisContext.readRowHolder().getRowIndex();
        if (!Objects.isNull(rowIndex) && rowIndex <= 48) {
            companyVO.setCreateUser("excel导入人账号信息");
            companyVOList.add(companyVO);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("excel读取结束！！！");
    }
}
